package org.lagou.edu.service;

import org.lagou.edu.dao.ProductDao;
import org.lagou.edu.logSdk.utils.OperationLogUtils;
import org.lagou.edu.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductService {

    @Autowired
    private OperationLogUtils<Product> operationLogUtils;

    @Autowired
    private ProductDao productDao;

    public Product getProductById(long id) throws Exception {
        return productDao.getById(id);
    }

    public void addProduct(Product product) throws Exception {
        productDao.insert(product);
        operationLogUtils.logAdd(product);
    }

    public void deleteProductById(long id) throws Exception {
        Product product = productDao.getById(id);
        productDao.deleteById(id);
        operationLogUtils.logDelete(product);
    }

    public void updateProduct(Product product) throws Exception {
        Product originProduct = getProductById(product.getId());
        productDao.update(product);
        operationLogUtils.logUpdate(originProduct, product);
    }



}
